package com.grace.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.grace.common.Result;
import com.grace.pojo.ServerInformation;
import com.grace.pojo.User;
import com.grace.service.IndexService;

/**
 * 
*    
* 类名称：IndexController   
* 类描述：   index界面控制器
* 创建人：zhourunmin   
* 创建时间：2017年6月27日 上午10:34:14  
* 备注：   
* @version    
*
 */
@Controller
@RequestMapping("/index")
public class IndexController {
	@Autowired
	private IndexService indexService;
	
	@RequestMapping(value="/header",method = RequestMethod.POST)
	@ResponseBody
	public Result header(HttpServletRequest request){
		HttpSession session = request.getSession();
		User user =null;
		user = (User)session.getAttribute("user");
		if (user == null) {
			return new Result(104,"session中没有user");
		}
		return new Result(200,"成功",user);
	}
	@RequestMapping(value="/welcome",method = RequestMethod.POST)
	@ResponseBody
	public Result welcome(HttpServletRequest request){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ServerInformation serverInformation = new ServerInformation();
		Properties props = System.getProperties();
        //java版本
        String javaVersion = props.getProperty("java.version");
        //操作系统名称
        String osName = props.getProperty("os.name") + props.getProperty("os.version");
        //用户的主目录
        String userHome = props.getProperty("user.home");
        //用户的当前工作目录
        String userDir = props.getProperty("user.dir");
        //服务器IP
        String serverIP = request.getLocalAddr();
        //客户端IP
        String clientIP = request.getRemoteHost();
        //WEB服务器
        String webVersion = request.getServletContext().getServerInfo();
        //CPU个数
        String cpuNum = Runtime.getRuntime().availableProcessors() + "核";
        //虚拟机内存总量
        String totalMemory = (Runtime.getRuntime().totalMemory()/1024/1024) + "M";
        //虚拟机空闲内存量
        String freeMemory = (Runtime.getRuntime().freeMemory()/1024/1024) + "M";
        //虚拟机使用的最大内存量
        String maxMemory = (Runtime.getRuntime().maxMemory()/1024/1024) + "M";
        //服务器用户账号
        String username = props.getProperty("user.name");
        //当前时间
        String currentTime = simpleDateFormat.format(new Date()).toString();
        //当前session总数
        String sessionNum = "";
        //当前sessionID
		String sessionId = request.getSession().getId();
		
		serverInformation.setClientIP(clientIP);
		serverInformation.setCpuNum(cpuNum);
		serverInformation.setCurrentTime(currentTime);
		serverInformation.setFreeMemory(freeMemory);
		serverInformation.setJavaVersion(javaVersion);
		serverInformation.setMaxMemory(maxMemory);
		serverInformation.setOsName(osName);
		serverInformation.setServerIP(serverIP);
		serverInformation.setSessionId(sessionId);
		serverInformation.setSessionNum(sessionNum);
		serverInformation.setTotalMemory(totalMemory);
		serverInformation.setWebVersion(webVersion);
		serverInformation.setUsername(username);
		serverInformation.setUserHome(userHome);
		serverInformation.setUserDir(userDir);
		
		return new Result(200,"serverInformation获取成功！",serverInformation);
		
	}
	
	
	
}
